/**
 * 
 */
package com.example.controller;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 
* controller基类
* @author: luochao
* @since: 2020年2月8日  下午2:04:17
* @history:
*/
public class BaseController {

    private static final String filterString = "BEGIN PRIVATE KEY|END PRIVATE KEY|BEGIN PUBLIC KEY|END PUBLIC KEY";

    /**
     * 读取文件
     *
     * @param fileName
     * @return 
     * @create  2020年2月7日 下午4:22:44 luochao
     * @history
     */
    protected String readFileContent(File file) {
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                //过滤非密钥部分
                if (filterString.indexOf(tempStr.replaceAll("-", "")) >= 0) {
                    continue;
                }
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }

    /***
     * 
     * assembleParam:组装参数.
     * 
     * @author luochao
     * @date 2018年1月27日 下午12:59:54
     * @param map 参数
     * @param contentType 参数类型
     * @return
     */
    protected static StringEntity assembleParam(Map<String, String> map, ContentType contentType) {
        StringEntity stringEntity = null;
        if (ContentType.APPLICATION_JSON.equals(contentType)) {
            stringEntity = new StringEntity(JSON.toJSONString(map), "UTF-8");
            stringEntity.setContentType("application/json");
        } else {
            List<NameValuePair> formparams = new ArrayList<NameValuePair>();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
            stringEntity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
        }

        return stringEntity;
    }

    /***
     * 
     * toString:httpEntity转换为字符串.
     * 
     * @author luochao
     * @date 2018年1月27日 下午1:34:33
     * @param httpEntity
     * @return
     */
    protected static String toString(HttpEntity httpEntity) {
        String result = null;
        try {
            result = EntityUtils.toString(httpEntity, Consts.UTF_8);
        } catch (ParseException | IOException e) {
            throw new RuntimeException("转字符串异常", e);
        }
        return result;
    }

    /**
     * 从HttpServletRequest中获取所有请求参数
     *
     * @param request
     * @return
     */
    public static Map<String, String> getRequestParmeter(HttpServletRequest request) {
        Map<String, String[]> requestParameter = request.getParameterMap();
        Map<String, String> map = new HashMap<>();
        try {
            for (String key : requestParameter.keySet()) {
                String[] array = requestParameter.get(key);
                String value = array != null && array.length > 0 ? array[0] : null;
                if (StringUtils.isNotBlank(value)) {
                    map.put(key, value);
                }
            }
        } catch (Exception e) {
            return map;
        }
        return map;
    }

}
